package com.sansan.qiangji.realize.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sansan.qiangji.VO.base.warehouseArea.WarehouseAreaListResultVO;
import com.sansan.qiangji.VO.base.warehouseArea.WarehouseAreaVO;
import com.sansan.qiangji.entity.base.WarehouseArea;
import com.sansan.qiangji.entity.base.example.WarehouseAreaExample;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.base.WarehouseAreaAddForm;
import com.sansan.qiangji.form.base.WarehouseAreaEditForm;
import com.sansan.qiangji.form.base.WarehouseAreaListForm;
import com.sansan.qiangji.service.inter.base.WarehouseAreaService;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import com.sansan.qiangji.utils.pageUtils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class WarehouseAreaReal {
    @Autowired
    private WarehouseAreaService warehouseAreaService;

    @Transactional
    public void deleteWarehouseArea(Integer warehouseAreaId){
        WarehouseArea warehouseArea = warehouseAreaService.selectByPrimaryKey(warehouseAreaId);
        if(warehouseArea==null){
            log.error("【仓库区域删除】仓库区域不存在，warehouseAreaId={}",warehouseAreaId);
            throw new ServiceException(ResultEnum.WAREHOUSEAREA_IS_NOT_EXIST);
        }
        int row = warehouseAreaService.deleteByPrimaryKey(warehouseAreaId);
        if(row==0){
            log.error("【仓库区域删除】仓库区域删除失败，warehouseAreaId={}",warehouseAreaId);
            throw new ServiceException(ResultEnum.WAREHOUSEAREA_DELETE_ERROR);
        }
    }

    @Transactional
    public void addWarehouseArea(WarehouseAreaAddForm warehouseAreaAddForm){
        WarehouseAreaExample example = new WarehouseAreaExample();
        example.or().andWarehouseAreaCodeEqualTo(warehouseAreaAddForm.getWarehouseAreaCode());
        example.or().andWarehouseAreaNameEqualTo(warehouseAreaAddForm.getWarehouseAreaName());
        List<WarehouseArea> warehouseAreaList = warehouseAreaService.selectByExample(example);
        if(warehouseAreaList.size()>0){
            log.error("【仓库区域新增】该仓库区域名称或编号已存在，warehouseAreaName={}",warehouseAreaAddForm.getWarehouseAreaName());
            throw new ServiceException(ResultEnum.WAREHOUSEAREA_IS_EXIST);
        }
        WarehouseArea warehouseArea = new WarehouseArea();
        BeanUtils.copyProperties(warehouseAreaAddForm,warehouseArea);
        int row = warehouseAreaService.insert(warehouseArea);
        if(row==0){
            log.error("【仓库区域新增】仓库区域新增失败");
            throw new ServiceException(ResultEnum.WAREHOUSEAREA_INSERT_ERROR);
        }
    }


    @Transactional
    public void editWarehouseArea(WarehouseAreaEditForm warehouseAreaEditForm){
        WarehouseArea warehouseArea = new WarehouseArea();
        BeanUtils.copyProperties(warehouseAreaEditForm,warehouseArea);
        int row = warehouseAreaService.updateByPrimaryKey(warehouseArea);
        if(row==0){
            log.error("【仓库区域新增】仓库区域新增失败");
            throw new ServiceException(ResultEnum.WAREHOUSEAREA_INSERT_ERROR);
        }
    }

    public WarehouseAreaListResultVO warehouseAreaList(WarehouseAreaListForm warehouseAreaListForm){
        WarehouseAreaExample example = new WarehouseAreaExample();
        int pageNum = warehouseAreaListForm.getPageNum();
        int pageSize = warehouseAreaListForm.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<WarehouseArea> warehouseAreaList = warehouseAreaService.selectByExample(example);
        PageResult<WarehouseArea> warehouseAreaPageResult = PageUtil.getPageResult(new PageInfo<>(warehouseAreaList));
        WarehouseAreaListResultVO warehouseAreaListResultVO = new WarehouseAreaListResultVO();
        BeanUtils.copyProperties(warehouseAreaPageResult,warehouseAreaListResultVO);
        List<WarehouseAreaVO> warehouseAreaVOS = new ArrayList<>();
        for(WarehouseArea warehouseArea : warehouseAreaPageResult.getContent()){
            WarehouseAreaVO warehouseAreaVO = new WarehouseAreaVO();
            BeanUtils.copyProperties(warehouseArea,warehouseAreaVO);
            warehouseAreaVOS.add(warehouseAreaVO);
        }
        warehouseAreaListResultVO.setWarehouseAreaVOS(warehouseAreaVOS);
        return warehouseAreaListResultVO;
    }


}
